import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store/index'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index',
  },
  {
    path: '/index',
    component: () => import('../views/Index')
  },
  {
    path: '/contact',
    component: () => import('../views/Contact')
  },
  {
    path: '/discovery',
    component: () => import('../views/Discovery')
  },
  {
    path: '/setting',
    component: () => import('../views/Setting')
  },
  {
    path: '/home',
    component: () => import('../views/Home')
  },
  {
    path: '/login',
    component: () => import('../views/Login')
  },
  {
    path: '/register',
    component: () => import('../views/Register')
  },
  {
    path: '/forgetpwd',
    component: () => import('../views/ForgetPassword')
  },
  {
    path: '/friend',
    component: () => import('../views/Friend')
  },
  {
    path: '/friendRequest',
    component: () => import('../views/FriendRequest')
  },
  {
    path: '/user',
    name: 'user',
    component: () => import('../views/User')
  },
  {
    path: '/chat',
    name: 'chat',
    component: () => import('../views/Chat')
  },
  {
    path: '/video',
    component: () => import('../views/Video')
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  let token = store.state.token;
  if (to.path === '/login' || to.path === '/home' || to.path == '/register') {
    //不处理，进入下一个拦截
    next();
  } else {
    if (token == null) {
      next('/home');
    } else {
      next();
    }
  }
})

export default router
